import { ComponentClass } from "breeze-leaf-core-lib";
import { breezeLeafStore } from "../utils/constant";

export class MetaManager {

  protected static instance: MetaManager;
  private currentMetaName: string = breezeLeafStore;

  constructor() {
    return MetaManager.instance || (MetaManager.instance = this);
  }

  protected allMetaComponent = {};

  setMetaComponent(name: string, json: ComponentClass) {
    this.allMetaComponent[name] = json;
  }

  getMetaComponentByName(name: string) {
    const json = this.allMetaComponent[name];
    if (!json) {
      throw new Error(`${json} meta is not found!`);
    }
    return json;
  }

  fetchMate(name: string) {
    //TODO 远程获取meta
  }

  setMetaName(name: string) {
    this.currentMetaName = name;
  }
  getMetaName() {
    return this.currentMetaName;
  }
}



export const registerMetaComponent = (name: string, json: ComponentClass) => {
  new MetaManager().setMetaComponent(name, json);
}

export const getMetaComponent = (name: string) => {
  return new MetaManager().getMetaComponentByName(name);
}